package com.boyon.common.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import com.boyon.common.domain.UserSession;
import com.boyon.common.session.SessionManager;
import com.boyon.common.status.Constant;
import com.boyon.web.interceptor.Filter;

import lombok.extern.slf4j.Slf4j;

/**
 * ClassName:LoginFilter <br/>
 * Function: 登录过滤器. <br/>
 * Date: 2018年3月19日 下午3:31:37 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Component
@Slf4j
public class LoginFilter implements Filter {

	private static final String filter = "boyon";

	/**
	 * .
	 * 
	 * @see com.boyon.web.interceptor.Filter#preHandle(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) {
		String uri = request.getRequestURI();
		log.debug("请求uri={}", uri);
		if (uri.contains(filter)) {
			Object obj = request.getSession()
					.getAttribute(Constant.SESSION_USER);
			if (obj == null) {
				log.debug("用户没有登录");
				return false;
			} else {
				SessionManager.setUserSession((UserSession) obj);
				return true;
			}
		}
		return true;
	}

	/**
	 * .
	 * 
	 * @see com.boyon.web.interceptor.Filter#postHandle(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.web.servlet.ModelAndView)
	 */
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) {

	}

	/**
	 * .
	 * 
	 * @see com.boyon.web.interceptor.Filter#afterCompletion(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      java.lang.Exception)
	 */
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {

	}

	/**
	 * .
	 * 
	 * @see com.boyon.web.interceptor.Filter#order()
	 */
	public int order() {
		return 0;
	}

}
